package br.unicamp.ic.lis.docseaservice.model.queryresult;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.log4j.Logger;

import br.unicamp.ic.lis.docseaservice.model.vo.LocationUri;

import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;

public class LocationUriHandler implements ResultSetHandler<LocationUri> {

	private static Logger LOG = Logger.getLogger(LocationUriHandler.class);

	@Override
	public LocationUri handle(ResultSet rs) throws SQLException {
		LocationUri lu = new LocationUri();
		rs.next();
		lu.setId(rs.getLong("id"));
		try {
			lu.setLocation(new WKTReader().read(rs.getObject("location")
					.toString()));
		} catch (ParseException e) {
			LOG.error("Failed to parse geometry type", e);
		}
		lu.setUri(rs.getString("uri"));
		lu.setCountry(rs.getString("country"));
		lu.setState(rs.getString("state"));
		lu.setCity(rs.getString("city"));

		return lu;
	}

}
